﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Utility
{
    public class UrlHelper
    {
        /// <summary>
        /// 返回URL网址
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUrl()
        {
            return HttpContext.Current.Request.Url.AbsoluteUri;
        }
        /// <summary>
        /// 得到当前网址的文件名部分
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUrlFileName()
        {
            string strUrl;
            strUrl = GetCurrentUrl();
            strUrl = strUrl.Substring(strUrl.LastIndexOf('/') + 1);
            return strUrl;
        }
        /// <summary>
        /// 返回当前不带参的网址
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUrlNoQueryString()
        {
            string strUrl;
            strUrl = GetCurrentUrl();
            strUrl = strUrl.Substring(0, strUrl.Length - (strUrl.LastIndexOf('?') + 1));
            return strUrl;
        }
        /// <summary>
        /// 返回无文件名的网址部分
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUrlNoFileName()
        {
            string strUrl = GetCurrentUrl();
            strUrl = strUrl.Substring(0, strUrl.LastIndexOf("/") + 1);
            return strUrl;
        }
        /// <summary>
        /// 返回当前网址部分
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentDomainName()
        {
            string strUrl;
            if (HttpContext.Current.Request.ServerVariables["Server_Port"].ToString() == "80")
            {
                strUrl = "Http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString() + "/";
            }
            else
            {
                strUrl = "Http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString() + ":" + HttpContext.Current.Request.ServerVariables["Server_Port"].ToString() + "/";
            }
            return strUrl;
        }
        /// <summary>
        /// 返回前一页的Url
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        ///测试的url地址:http://www.test.com/testweb/default.htm, 结果如下：
        ///Request.ApplicationPath: /testweb
        ///Request.CurrentExecutionFilePath: /testweb/default.htm
        ///Request.FilePath: /testweb/default.htm
        ///Request.Path: /testweb/default.htm
        ///Request.PathInfo: 
        ///Request.PhysicalApplicationPath: E:\WWW\testweb\
        ///Request.PhysicalPath: E:\WWW\testweb\default.htm
        ///Request.RawUrl: /testweb/default.htm
        ///Request.Url.AbsolutePath: /testweb/default.htm
        ///Request.Url.AbsoluteUri: http://www.test.com/testweb/default.htm
        ///Request.Url.Host: www.test.com
        ///Request.Url.LocalPath: /testweb/default.htm
        /// </remarks>
        public static string GetForwardUrl()
        {
            return HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
        }
    }
}
